package com.sxm.yiwei.fragments;


import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.VendorAbnormolActivity;
import com.sxm.yiwei.activitis.VendorDiscussActivity;
import com.sxm.yiwei.activitis.VendorInserviceActivity;
import com.sxm.yiwei.activitis.VendorNoOverActivity;
import com.sxm.yiwei.activitis.VendorNoServiceActivity;
import com.sxm.yiwei.activitis.VendorOverActivity;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 *  卖家订单
 * A simple {@link Fragment} subclass.
 */


public class ShopVendorFragment extends Fragment implements View.OnClickListener{

    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String complaintCount;
    private String end;
    private String complete;
    private String pendingService;
    private String inService;
    private TextView vendorservice1,vendorservice2,vendorservice3,vendorservice4,vendorservice5;

    public ShopVendorFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shop_vendor, container, false);
        initVendorView(view);

        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        getVendorBookList();

        return view;

    }

    @Override
    public void onStart() {
        super.onStart();
        getVendorBookList();
    }

    //获取卖家列表
    public void getVendorBookList() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETVENDORBOOKNUMBER);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");
                JSONObject object1 = JSON.parseObject(data);
                end = object1.getString("end"); // 已完结
                complaintCount = object1.getString("complaintCount");//异常订单
                complete = object1.getString("complete");// 已完成
                pendingService = object1.getString("pendingService"); //待服务
                inService = object1.getString("inService");// 服务中
                if (pendingService==null){

                    vendorservice1.setText("0");
                }else {
                    vendorservice1.setText(pendingService);

                }
                if (inService==null){
                    vendorservice2.setText("0");

                }else {
                    vendorservice2.setText(inService);

                }
                if (complete==null){
                    vendorservice3.setText("0");

                }else {
                    vendorservice3.setText(complete);

                }
                if (end==null){
                    vendorservice4.setText("0");

                }else {
                    vendorservice4.setText(end);

                }
                if (complaintCount==null){
                    vendorservice5.setText("0");
                }else {
                    vendorservice5.setText(complaintCount);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initVendorView(View view) {
        RelativeLayout noservicer = (RelativeLayout) view.findViewById(R.id.vendor_noservice);
        RelativeLayout inservice = (RelativeLayout) view.findViewById(R.id.vendor_inservice);

        RelativeLayout noover = (RelativeLayout) view.findViewById(R.id.vendor_noover);
        RelativeLayout over = (RelativeLayout) view.findViewById(R.id.vendor_over);
        RelativeLayout abnormol = (RelativeLayout) view.findViewById(R.id.vendor_abnormol);
        vendorservice1 = (TextView) view.findViewById(R.id.vendor_service1);
        vendorservice2 = (TextView) view.findViewById(R.id.vendor_service2);
        vendorservice3 = (TextView) view.findViewById(R.id.vendor_service3);
        vendorservice4 = (TextView) view.findViewById(R.id.vendor_service4);
        vendorservice5 = (TextView) view.findViewById(R.id.vendor_service5);

        noservicer.setOnClickListener(this);
        inservice.setOnClickListener(this);
        noover.setOnClickListener(this);
        over.setOnClickListener(this);
        abnormol.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.vendor_noservice:
                startActivity(new Intent(getActivity(), VendorNoServiceActivity.class));
                break;
            case R.id.vendor_inservice:
                startActivity(new Intent(getActivity(), VendorInserviceActivity.class));
                break;
            case R.id.vendor_noover:
                startActivity(new Intent(getActivity(), VendorNoOverActivity.class));
                break;
            case R.id.vendor_over:
                startActivity(new Intent(getActivity(), VendorOverActivity.class));
                break;
            case R.id.vendor_abnormol:
                startActivity(new Intent(getActivity(), VendorAbnormolActivity.class));
                break;
        }
    }
}
